package com.goods.business.service.imp;

import com.goods.business.mapper.ProductListMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductListService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/9 13:52 Administrator
 * @Description
 */
@Service
public class ProductListServiceImpl implements ProductListService {
    @Autowired
    private ProductListMapper productListMapper;
    @Autowired
    private ProductStockMapper productStockMapper;
    /**
     * 分页展示物资资料数据
     * @param pageNum
     * @param pageSize
     * @param
     * @param
     * @return
     */
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, Map map) {
        String categoryId = (String) map.get("categoryId");
        //categoryId=&supplier=&status=0&categorys=24,30,62
        String[] split = categoryId.split(",");
        if (split.length==3){//三个ID都有
            map.put("three_category_id",split[2]);
            map.put("two_category_id",split[1]);
            map.put("one_category_id",split[0]);
        }else if (split.length==2){//只有一级和二级ID
            map.put("two_category_id",split[1]);
            map.put("one_category_id",split[0]);
        }else if (split.length==1){//只有一级ID
            map.put("one_category_id",split[0]);
        }

        List<Product> productList =productListMapper.findproductList(map);
        if (!CollectionUtils.isEmpty(productList)){
            List<ProductVO> productVOList = productList.stream().map(product -> {
                ProductVO productVO = new ProductVO();
                BeanUtils.copyProperties(product,productVO);
                return productVO;
            }).collect(Collectors.toList());
            //获取总条数
            int total=productVOList.size();
            //获取每页记录数
            int size=(pageNum-1)*pageSize+pageSize;
            List<ProductVO> productVOList1=productVOList.subList((pageNum-1)*pageSize,size>total?total:size);
            PageVO<ProductVO> pageVO = new PageVO<>(total,productVOList1);
            return pageVO;
        }

        return null;
    }

    /**
     * 添加物资资料
     * @param productVO
     */
    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        String uuid = UUID.randomUUID().toString();
        productVO.setPNum(uuid);
      Long[] categoryKeys = productVO.getCategoryKeys();
      for (Long categoryKey : categoryKeys) {
          if (categoryKeys[0]==categoryKey){
              productVO.setOneCategoryId(categoryKey);
          }else if (categoryKeys[1]==categoryKey){
              productVO.setTwoCategoryId(categoryKey);
          }else if (categoryKeys[2]==categoryKey){
              productVO.setThreeCategoryId(categoryKey);
          }
        }
      productVO.setStatus(2);
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        productListMapper.insert(product);
    }

    /**
     * 编辑物资用于做修改时的数据回显
     * @param id
     * @return
     */
    @Override
    public ProductVO edit(Long id) {
        Product product = productListMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return productVO;
    }

    /**
     * 修改物资资源
     * @param id
     * @param productVO
     */
    @Override
    public void update(Long id, ProductVO productVO) {
        Product product = productListMapper.selectByPrimaryKey(id);
        if (!product.getName().equals(productVO.getName())){//如果两个的商品名称不一样说明更改了商品反之没更改
            String uuid = UUID.randomUUID().toString();
            product.setPNum(uuid);
        }
        Long[] categoryKeys = productVO.getCategoryKeys();
        for (Long categoryKey : categoryKeys) {
            if (categoryKeys[0]==categoryKey){
                productVO.setOneCategoryId(categoryKey);
            }else if (categoryKeys[1]==categoryKey){
                productVO.setTwoCategoryId(categoryKey);
            }else if (categoryKeys[2]==categoryKey){
                productVO.setThreeCategoryId(categoryKey);
            }
        }
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());
      productListMapper.updateByPrimaryKey(product);
    }

    /**
     * 将物资资源移入回收站
     * @param id
     */
    @Override
    public void remove(Long id) {
        Product product = productListMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        productListMapper.updateByPrimaryKey(product);
    }

    /**
     *  //将回收站中的东西恢复
     * @param id
     */
    @Override
    public void back(Long id) {
        Product product = productListMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productListMapper.updateByPrimaryKey(product);
    }

    /**
     * 将回收站的东西删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        productListMapper.deleteByPrimaryKey(id);
    }

    /**
     * 分页展示，入库时的物资资料数据
     * @param pageNum
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public PageVO<ProductVO> findProducts(Integer pageNum, Integer pageSize, Map map) {
        String categoryId = (String) map.get("categorys");
        if (categoryId!=null){
            //categoryId=&supplier=&status=0&categorys=24,30,62
            String[] split = categoryId.split(",");
            if (split.length==3){//三个ID都有
                map.put("three_category_id",split[2]);
                map.put("two_category_id",split[1]);
                map.put("one_category_id",split[0]);
            }else if (split.length==2){//只有一级和二级ID
                map.put("two_category_id",split[1]);
                map.put("one_category_id",split[0]);
            }else if (split.length==1){//只有一级ID
                map.put("one_category_id",split[0]);
            }
        }


        List<Product> productList =productListMapper.findproductList(map);
        if (!CollectionUtils.isEmpty(productList)){
            List<ProductVO> productVOList = productList.stream().map(product -> {
                ProductVO productVO = new ProductVO();
                BeanUtils.copyProperties(product,productVO);
                return productVO;
            }).collect(Collectors.toList());
            //获取总条数
            int total=productVOList.size();
            //获取每页记录数
            int size=(pageNum-1)*pageSize+pageSize;
            List<ProductVO> productVOList1=productVOList.subList((pageNum-1)*pageSize,size>total?total:size);
            PageVO<ProductVO> pageVO = new PageVO<>(total,productVOList1);
            return pageVO;
        }

        return null;
    }

    /**
     * 物资库存
     * @param pageNum
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, Map map) {
        String categoryId = (String) map.get("categorys");
        if (categoryId!=null){
            //categoryId=&supplier=&status=0&categorys=24,30,62
            String[] split = categoryId.split(",");
            if (split.length==3){//三个ID都有
                map.put("three_category_id",split[2]);
                map.put("two_category_id",split[1]);
                map.put("one_category_id",split[0]);
            }else if (split.length==2){//只有一级和二级ID
                map.put("two_category_id",split[1]);
                map.put("one_category_id",split[0]);
            }else if (split.length==1){//只有一级ID
                map.put("one_category_id",split[0]);
            }
        }


        List<Product> productList =productListMapper.findproductList(map);
        if (!CollectionUtils.isEmpty(productList)){
            List<ProductStockVO> productStockVOList = productList.stream().map(product -> {
                String pNum = product.getPNum();
                ProductStockVO productStockVO = new ProductStockVO();

                ProductStock productStock = productStockMapper.selectBypNum(pNum);
               if (productStock!=null){
                   productStockVO.setStock(productStock.getStock());
               }

                BeanUtils.copyProperties(product,productStockVO);
                return productStockVO;
            }).collect(Collectors.toList());
            //获取总条数
            int total=productStockVOList.size();
            //获取每页记录数
            int size=(pageNum-1)*pageSize+pageSize;
            List<ProductStockVO> productStockVOList1=productStockVOList.subList((pageNum-1)*pageSize,size>total?total:size);
            PageVO<ProductStockVO> pageVO = new PageVO<>(total,productStockVOList1);
            return pageVO;
        }

        return null;
    }


}
